package org.train;

import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.EmptyBorder;

import org.ConfigLoader;

@SuppressWarnings("serial")
public class TrainPanel extends JFrame implements Train {
	private Integer trainSpeed = new Integer(ConfigLoader.configBundle.getString("train.speed"));
	private JPanel mainPane;
	private JLabel lblAbc;
	private boolean stringState;
	private JPanel trainPanel;
	private JScrollPane logPane;
	private JTextArea logArea;
	private DateFormat sdf = new SimpleDateFormat("HH:mm:sss");

	public TrainPanel() {
		setResizable(false);
		setTitle("Trem");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 370, 262);
		setMainPane(new JPanel());
		getMainPane().setBackground(Color.BLACK);
		getMainPane().setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(getMainPane());
		Font fonte = new Font("Arial", Font.BOLD, 40);
		getMainPane().setLayout(new GridLayout(2, 1, 0, 0));
		
		setTrainPanel(new JPanel());
		getTrainPanel().setBackground(Color.BLACK);
		getMainPane().add(getTrainPanel());
		
		setLblAbc(new JLabel("abc"));
		getLblAbc().setBackground(Color.BLACK);
		getLblAbc().setForeground(Color.GREEN);
		getLblAbc().setFont(fonte);
		
		setLogPane(new JScrollPane());
		getMainPane().add(getLogPane());
		
		setLogArea(new JTextArea());
		getLogArea().setBackground(Color.WHITE);
		getLogArea().setEditable(false);
		getLogArea().setCaretPosition(getLogArea().getDocument().getLength());
		getLogPane().setViewportView(getLogArea());
	}
	
	synchronized public void moveString() {
		setStringState(true);
		Runnable r = new Runnable() {
			public void run() {
				int cpWidth = getContentPane().getWidth();
				int counter = 0;
				while (getStringState()) {
					counter++;
					if (counter > cpWidth) {
						getLblAbc().setBounds(getLblAbc().getWidth() * -1, getLblAbc().getY(), 80, 50);
						counter = 0;
					}
					getLblAbc().setBounds(getLblAbc().getX() + 1, getLblAbc().getY(), 80, 50);
					try {
						Thread.sleep(getTrainSpeed());
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		};
		new Thread(r).start();
	}
	
	@Override
	synchronized public void logMessage(String message, boolean logTime) {
		String oldText = getLogArea().getText();
		String timeLog = "";
		if (logTime) timeLog = "     --> HH:mm:sss: " + sdf.format(new Date());
		getLogArea().setText( message + timeLog + "\n" + oldText);
	}
	
	public void pauseString() {
		setStringState(false);
	}
	
	/*
	 * GETTERS AND SETTERS
	 */
	
	public JLabel getLblAbc() {
		return lblAbc;
	}

	public boolean getStringState() {
		return stringState;
	}

	public void setStringState(boolean stringStopped) {
		this.stringState = stringStopped;
	}

	public Integer getTrainSpeed() {
		return trainSpeed;
	}

	public void setTrainSpeed(Integer trainSpeed) {
		this.trainSpeed = trainSpeed;
	}

	private void setLblAbc(JLabel lblAbc) {
		getTrainPanel().setLayout(null);
		this.lblAbc = lblAbc;
		lblAbc.setBounds(10, 34, 68, 47);
		getTrainPanel().add(lblAbc);
	}

	private JPanel getMainPane() {
		return mainPane;
	}

	private void setMainPane(JPanel mainPane) {
		this.mainPane = mainPane;
	}

	private JPanel getTrainPanel() {
		return trainPanel;
	}

	private void setTrainPanel(JPanel trainPanel) {
		this.trainPanel = trainPanel;
	}
	public JScrollPane getLogPane() {
		return logPane;
	}
	public JTextArea getLogArea() {
		return logArea;
	}

	public void setLogPane(JScrollPane logPane) {
		this.logPane = logPane;
	}

	public void setLogArea(JTextArea logArea) {
		this.logArea = logArea;
	}
}